<template>
  <!-- 题型模块管理 -->
  <div class="app-container">
    <!-- 模糊搜索模块 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <!-- 模块名称输入框 -->
      <el-form-item label="模块:" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模块名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 科目下拉框 -->
      <el-form-item label="科目:" prop="subjectId">
        <el-select
          v-model="queryParams.subjectId"
          placeholder="请选择科目"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in kaolist"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <!-- 题型下拉框 -->
      <el-form-item label="题型:" prop="questionTypeId">
        <el-select
          v-model="queryParams.questionTypeId"
          placeholder="请选题型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in topicList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <!-- 状态下拉框 -->
      <el-form-item label="状态:" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in status"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <!-- 搜索重置按钮 -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 新增按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:module:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- 科目表 -->
    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
      :row-class-name="tableRowClassName"
    >
      <el-table-column
        label="序号"
        type="index"
        width="70"
        :index="indexMetthod"
        align="center"
      >
      </el-table-column>
      <el-table-column label="模块" align="center" prop="name" />
      <el-table-column label="科目" align="center" prop="subject" />
      <el-table-column label="题型" align="center" prop="questionType" />
      <el-table-column label="新增人" align="center" prop="createOperator" />
      <el-table-column label="新增时间" align="center" prop="createTime" />
      <el-table-column label="操作人" align="center" prop="updateOperator" />
      <el-table-column label="操作时间" align="center" prop="updateTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">禁用</span>
          <span v-else-if="scope.row.status == 1">启用</span>
          <span v-else>暂无状态</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:module:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:module:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改科目信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- 科目下拉框 -->
        <el-form-item label="科目" prop="subjectId">
          <el-select v-model="form.subjectId" placeholder="请选择">
            <el-option
              v-for="item in kaolist"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!-- 题型下拉框 -->
        <el-form-item label="题型" prop="questionTypeId">
          <el-select v-model="form.questionTypeId" placeholder="请选择">
            <el-option
              v-for="item in topicList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!-- 模块输入框 -->
        <el-form-item label="模块" prop="name">
          <el-input
            v-model.trim="form.name"
            placeholder="请输入模块"
            maxlength="80"
          />
        </el-form-item>
        <!-- 状态单选 -->
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <!-- 确定 取消 按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listType,
  topicType,
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  forbidden,
} from "@/api/system/module";

export default {
  name: "classify",
  data() {
    return {
      // 启用科目下拉列表
      kaolist: [],
      // 题型下拉列表
      topicList: [],
      //状态
      status: [
        {
          id: 1,
          name: "启用",
        },
        {
          id: 0,
          name: "禁用",
        },
      ],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科目信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        subjectId:null,
        questionTypeId:null,
        status:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        subjectId: [
          { required: true, message: "请选择科目", trigger: "change" },
        ],
        questionTypeId: [
          { required: true, message: "请选择题型", trigger: "change" },
        ],
        topic: [{ required: true, message: "请选择", trigger: "change" }],
        examType: [
          { required: true, message: "请选择题型", trigger: "change" },
        ],
        name: [{ required: true, message: "模块不能为空", trigger: "blur" }],
        subject: [{ required: true, message: "请选择", trigger: "change" }],
      },
      // 禁用启用
      statuslist: {
        id: null,
        status: null,
      },
    };
  },
  created() {
    this.getList();
    this.xialaList();
    this.xiala2List();
  },
  methods: {
    // 隔行效果
    tableRowClassName({ row, rowIndex }) {
      if ((rowIndex + 1) % 2 === 0) {
        return "warning-row";
      } else {
        return "success-row";
      }
    },
    // 序号
    indexMetthod: function (index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    xialaList() {
      this.loading = true;
      listType().then((response) => {
        this.kaolist = response.rows;
        this.loading = false;
        console.log(this.kaolist, "下拉0列表~  科目~~~~~~~~~~~~~~~~");
      });
    },
    xiala2List() {
      this.loading = true;
      topicType().then((response) => {
        this.topicList = response.rows;
        this.loading = false;
        console.log(this.topicList, "下拉0列表~  题型~~~~~~~~~~~~~~~~");
      });
    },
    /** 查询科目信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.infoList, "模块~~~~~~~~~~~~~~");
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        examTypeId: null,
        jobId: null,
        name: null,
        price: null,
        classPeriod: null,
        speaker: null,
        classPeriodConfType: null,
        invoiceName: null,
        classPeriodSign: null,
        createBy: null,
        createTime: null,
        status: "0",
        order: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.examTypeId = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加题型模块";
      // this.$nextTick(() => {
      //   this.$refs.gain.focus();
      // });
      this.form.status = 1;
    },
    // 启用按钮
    handlestart(row) {
      console.log(row);
      let statuslist = this.statuslist;
      statuslist.id = row.id;
      statuslist.status = row.status;
      if (statuslist.status == 0) {
        statuslist.status = 1;
      }
      forbidden(statuslist).then((response) => {
        this.getList();
      });
    },
    //禁用按钮
    handleforbidden(row) {
      console.log(row);
      let statuslist = this.statuslist;
      statuslist.id = row.id;
      statuslist.status = row.status;
      if (statuslist.status == 1) {
        statuslist.status = 0;
      }
      console.log(statuslist, "statuslist");
      forbidden(statuslist).then((response) => {
        this.getList();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改题型模块";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(888, this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.$confirm("是否确认修改数据？", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                updateInfo(this.form).then((response) => {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
              })
              .catch(() => {});
          } else {
            this.$confirm("是否确认新增数据？", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                addInfo(this.form).then((response) => {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              })
              .catch(() => {});
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除当前数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
